package com.changfang.gold_man.common;

import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageUtil {

    //图片转二进制数组
    public static byte[] getImageBinary(String path, String imgType) {
        File f = new File(path);
        BufferedImage bi;
        try {
            bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, imgType, baos);
            byte[] imgBytes = baos.toByteArray();
            return imgBytes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //图片存服务器，返回映射地址
    public static String fileUpload(MultipartFile file) {
//        String BaseURL = "http://localhost:8080";
        String BaseURL = "http://121.37.209.61:8080";

        if(file == null){
            return null;
        }
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);

        String uploadpath = "/home/upload/";
//        String uploadpath = "C:\\Users\\user\\Desktop\\";

        try{
            FileUtil.uploadFiles(file.getBytes(), uploadpath,fileName);
        }catch (Exception e){
            System.out.println(e);
        }
        //localhost:8080,Tomcat文件映射
        String url = BaseURL +"/upload/"+ fileName;
        return url;
    }
}
